package com.yuchen.ishop.web.app.seller;


import com.yuchen.ishop.Gobals;
import com.yuchen.ishop.entity.*;
import com.yuchen.ishop.service.seller.SellerRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping(value = "/my/seller")
public class SellerAction extends BaseAction {

    private final static Logger LOG = Logger.getLogger(SellerAction.class);

    @Autowired
    private SellerRestClient sellerRestClient;



    @RequestMapping(value = "index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "my/seller/index";
    }





    //个人信息 跳转
    @RequestMapping(value = "goUserInfo")
    public String goUserInfo(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        if("seller".equals(userInfo.getRole())){
            return "redirect:/my/seller/sellerInfo.htm";
        }else if("xiaoer".equals(userInfo.getRole())){
            return "redirect:/my/seller/xiaoerInfo.htm";
        }else {
            return "redirect:/error.htm";
        }
    }

    @RequestMapping(value = "sellerInfo")
    public String sellerInfo(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        if("seller".equals(userInfo.getRole())){
            modelMap.addAttribute("myMenu", "userInfo");
            Seller seller = sellerRestClient.getById(userInfo.getId());
            seller.setPassword(null);
            seller.setSalt(null);
            modelMap.put("seller",seller);
            return "/my/seller/sellerInfo";
        }
        return "redirect:/error.htm";
    }

    //saveOrUpdateSeller
    @RequestMapping(value = "saveOrUpdateSeller",method = RequestMethod.POST)
    public String saveOrUpdateSeller(Seller seller, HttpServletRequest request,RedirectAttributes redirectAttributes) {
        if(seller.getId()!=null){
            sellerRestClient.update(seller);
            redirectAttributes.addFlashAttribute(Gobals.MSG,successJson());
            return "redirect:/my/seller/sellerInfo.htm";
        }
        return "redirect:/error.htm";
    }

    //
    @RequestMapping(value = "goPwd")
    public String goPwd(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        if("seller".equals(userInfo.getRole())){
            return "redirect:/my/seller/sellerPwd.htm";
        }else if("xiaoer".equals(userInfo.getRole())){
            return "redirect:/my/seller/xiaoerPwd.htm";
        }else {
            return "redirect:/error.htm";
        }
    }

    @RequestMapping(value = "sellerPwd")
    public String sellerPwd(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        CookieUserInfo userInfo = cookie2UserInfo(request);
        if("seller".equals(userInfo.getRole())){
            modelMap.addAttribute("myMenu", "password");
            Seller seller = sellerRestClient.getById(userInfo.getId());
            modelMap.put("sellerId",seller.getId());
            return "/my/seller/sellerPwd";
        }
        return "redirect:/error.htm";
    }


    @RequestMapping(value = "updateSellerPwd",method = RequestMethod.POST)
    public String updateSellerPwd(Long id,String password, HttpServletRequest request,RedirectAttributes redirectAttributes) {
        if(id!=null && StringUtils.isNotBlank(password)){
            Map map = new HashMap(2);
            map.put("id",id);
            map.put("password",password);
            sellerRestClient.updatePwd(map);
            redirectAttributes.addFlashAttribute(Gobals.MSG,successJson());
            return "redirect:/my/seller/sellerPwd.htm";
        }
        return "redirect:/error.htm";
    }



}
